﻿using BO;
using BrightIdeasSoftware;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using UIControls;

namespace UI.Core
{
    public partial class ReportFormBase : FormBase
    {
        public ReportFormBase()
        {
            InitializeComponent();
           txtSearch.TextBox.TextChanged+=txtSearch_TextChanged;
            if (!this.DesignMode)
            {
                this.Load += BaseReportForm_Load;
            }
        }

      
    
        protected virtual IEnumerable MySource { get { return null; } }
        protected double TotalAmount { get; set; }

        protected DateTime BeginDate
        {
            get { return dateRange.BeginDate; }
        }
        protected DateTime EndDate
        {
            get { return dateRange.EndDate; }
        }

        protected virtual UIControls.Grid.OSGrid MyGrid { get { return _MyGrid; } }

        private void BaseReportForm_Load(object sender, EventArgs e)
        {
            dateRange.Range = DateRange.ThisMonth;
            setGrid();
            InitGrid();
            if (IsSecurityMode)
                return;
            FetchData();
            DataBind();
        }
        void setGrid()
        {
            MyGrid.UseFiltering = true;
            MyGrid.FullRowSelect = true;
            MyGrid.AllowColumnReorder = true;

            MyGrid.HeaderWordWrap = true;
            MyGrid.RowHeight = 25;
          
        }

        protected virtual void InitGrid()
        {
           
            
        }

        protected virtual void FetchData()
        {
           
        }

        protected virtual void DataBind()
        {
          
            MyGrid.SetObjects(MySource);
            lbTotal.Text = TotalAmount.ToString();
        }

        private void btnChart_Click(object sender, EventArgs e)
        {
            //MyGrid.Visible = false;
            //myChart.Visible = true;
            //myChart.Dock = DockStyle.Fill;

        }
        protected virtual void DisplayChart()
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            FetchData();
            DataBind();
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            var dt = MyGrid.GenerateDataTable();
            var excel = new ExcelAdapter();
            excel.SaveExcel(dt, "", this.Text, this.Text);
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            OnSearch();
        }
        virtual protected void OnSearch()
        {
            TimedFilter(MyGrid, SearchText);
        }
        virtual protected void OnGridFilterd(UIControls.Grid.OSGrid.FilterdEventArgs e)
        {

        }
        protected string SearchText { get { return txtSearch.Text; } }

        void TimedFilter(ObjectListView olv, string txt)
        {
            this.TimedFilter(olv, txt, 0);
        }

        void TimedFilter(ObjectListView olv, string txt, int matchKind)
        {
            TextMatchFilter filter = null;
            if (!String.IsNullOrEmpty(txt))
            {
                switch (matchKind)
                {
                    case 0:
                    default:
                        filter = TextMatchFilter.Contains(olv, txt);
                        break;
                    case 1:
                        filter = TextMatchFilter.Prefix(olv, txt);
                        break;
                    case 2:
                        filter = TextMatchFilter.Regex(olv, txt);
                        break;
                }
            }
            // Setup a default renderer to draw the filter matches
            if (filter == null)
                olv.DefaultRenderer = null;
            else
            {
                olv.DefaultRenderer = new HighlightTextRenderer(filter);

                // Uncomment this line to see how the GDI+ rendering looks
                //olv.DefaultRenderer = new HighlightTextRenderer { Filter = filter, UseGdiTextRendering = false };
            }

            // Some lists have renderers already installed
            HighlightTextRenderer highlightingRenderer = olv.GetColumn(0).Renderer as HighlightTextRenderer;
            if (highlightingRenderer != null)
                highlightingRenderer.Filter = filter;

            Stopwatch stopWatch = new Stopwatch();
            stopWatch.Start();
            olv.ModelFilter = filter;
            stopWatch.Stop();

            IList objects = olv.Objects as IList;

        }

        private void btnExport_Click_1(object sender, EventArgs e)
        {
            var ct = sender as Control;
            ct.Enabled = false;
            OnExport();
            ct.Enabled = true;
            Cursor = Cursors.Default;
        }
        virtual protected void OnExport()
        {
            var dt = MyGrid.GenerateDataTable();
            var excel = new ExcelAdapter();
            excel.SaveExcel(dt, "", this.Text, this.Text);
        }
    }
}
